﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DIPLib
{
    public partial class HistogramPanel : UserControl
    {
        private int[] values;
        bool isDrawing = false;
        Color mColor;
        public HistogramPanel()
        {
            InitializeComponent();
        }

        private void HistogramForm_Paint(object sender, PaintEventArgs e)
        {
            if (isDrawing)
            {
                Pen pen = new Pen(mColor);
                Graphics g = e.Graphics;
                int maxValue = getMaxValue();
                pen.Color = Color.Black;
                g.DrawLine(pen, 0, 150, 0, 0);
                g.DrawLine(pen, 0, 151, 260, 151);
                pen.Color = Color.Red;
                for (int i = 0; i < values.Length; i++)
                {
                    g.DrawLine(pen, i + 2, 150, i + 2, 150 - values[i] * Height / maxValue);
                }
            }
        }

        public void drawHistogramChart(int[] histogramArray, Color c)
        {
            values = new int[256];
            histogramArray.CopyTo(values, 0);
            isDrawing = true;
            mColor = c;
            Refresh();
        }

        private int getMaxValue()
        {
            int maxValue = 0;
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] > maxValue)
                {
                    maxValue = values[i];
                }
            }
            return maxValue;
        }
    }
}
